package spider.utils;

import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.JsonPath;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class JsonPathUtil {

    /**
     * https://github.com/json-path/JsonPath
     * @param data
     * @param jsonExpress
     * @param type
     * @param <T>
     * @return
     */
    public  static  <T> T analysisJsonData(String data, String jsonExpress, Class<T> type) {
        try {
            return JsonPath
                    .using(Configuration.defaultConfiguration())
                    .parse(data)
                    .read(jsonExpress, type);
        } catch (Exception e) {
            log.error("analysisJsonData data:" + data + ",jsonExpress:" + jsonExpress);
            return null;
        }
    }
}
